<template>
  <div class="content">
    <el-table
      :data="tableData"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center', padding: '7px' }"
      :row-class-name="tableRowClassName"
      v-loading="loading"
    >
      <el-table-column label="通道" prop="tunnelNum"></el-table-column>
      <el-table-column label="监测类型" prop="dataType"></el-table-column>
      <el-table-column label="测值" prop="realData"></el-table-column>
      <el-table-column label="单位">
        <template>
          <span>m^3</span>
        </template>
      </el-table-column>
      <el-table-column label="采集时间" prop="creatTime"></el-table-column>
    </el-table>
    <el-button @click="realTimeCollecting()" size="small" type="primary"
      >实时采集</el-button
    >
    <p class="pStyle">南京中网卫星通信股份有限公司</p>
  </div>
</template>

<script>
import Axios from "axios";
import moment from "moment";
import * as serviceApi from "../../../../common/service.config";
Axios.defaults.withcredentials = true;

export default {
  name: "Flow",
  props: {
    node: Object,
  },
  data() {
    return {
      tableData: null,
      loading: true,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      Axios({
        url: serviceApi.KB_API,
        method: "post",
        data: `appMsg={"parameter":"{\\"sensorNumber\\":\\"${this.node.sensorNumber}\\",\\"dataType\\":\\"01\\",\\"sensorName\\":\\"${this.node.sensorName}\\"}","serviceCode":"queryRealDataBySensorNo","timestamp":"","uuid":""}`,
      }).then((res) => {
        let list = res.data.parameter;
        for (let i = 0; i < list.length; i++) {
          list[i].tunnelNum = `通道${list[i].tunnelNum}`;
          list[i].creatTime = moment(Number(list[i].creatTime)).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          list[i].dataType = "流量";
        }
        this.tableData = list;
      });
      this.loading = false;
    },
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return "row01";
      } else {
        return "row02";
      }
    },
    realTimeCollecting() {
      this.loading = true;
      Axios({
        url: serviceApi.KB_API,
        method: "post",
        data: `appMsg={"parameter":"{\\"sensorNumber\\":\\"${this.node.sensorNumber}\\",\\"dataType\\":\\"01\\",\\"sensorName\\":\\"${this.node.sensorName}\\"}","serviceCode":"callingForRealData","timestamp":"","uuid":""}`,
      }).then((res) => {
        console.log(res);
        if (res.data.resultcode == "0000") {
          this.$message({
            message: "成功采集",
            type: "success",
            duration: 1000,
            onClose: () => {
              this.getList();
            },
          });
        } else {
          this.$message({
            message: "采集失败",
            type: "error",
            duration: 1000,
            onClose: () => {
              this.getList();
            },
          });
        }
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  height: 92vh;
  position: relative;
  .pStyle {
    position: absolute;
    bottom: 1%;
    right: 2%;
  }
  .el-table {
    /deep/ .row01 {
      background: #ffbab7;
    }
    /deep/ .row02 {
      background: #c7efcf;
    }
  }
  .el-button {
    margin: 2.05rem 50rem;
  }
}
</style>